<?php
namespace backend\models;
use Yii;
use \yii\db\ActiveRecord;

class Article extends ActiveRecord{
    public $tags;

    public static function tableName(){
        return '{{%article}}';
    }

    public function rules(){
        return [
            ['title', 'required', 'message' => '标题不能为空', 'on' => ['add_art']],
            ['source', 'required', 'message' => '来源不能为空', 'on' => ['add_art']],
            ['excerpt', 'required', 'message' => '摘要不能为空', 'on' => ['add_art']],
            ['details', 'required', 'message' => '内容不能为空', 'on' => ['add_art']],
            ['weight', 'required', 'message' => '权重不能为空', 'on' => ['add_art']],
            ['digest', 'required', 'message' => '请选择是否置顶', 'on' => ['add_art']],
            ['display', 'required', 'message' => '请选择是否显示', 'on' => ['add_art']],
            ['title', 'unique', 'message' => '标题已经使用过', 'on' => ['add_art']],
        ];
    }

    public function attributeLabels(){
        return [
            'title' => '标题',
            'category_id' => '文章板块',
            'source' => '来源',
            'image' => '图片',
            'excerpt' => '摘要',
            'details' => '内容',
            'weight' => '权重',
            'digest' => '置顶',
            'display' => '显示',
            'tags' => '标签',
            'create_time' => '添加时间'
        ];
    }

   public function getCategory(){
        return $this->hasOne(Category::className(), ['cateid' => 'category_id']);
    }

    public function beforeSave( $insert ){
       if( parent::beforeSave( $insert ) ){
           if( $this->isNewRecord ){
               $this->create_time = time();
           }
           return true;
       }
       return false;
   }
}